package io.gitee.shengzheng1999.esbcommon.common.utils;

import org.apache.commons.codec.binary.Base64;

/**
 * @className		: Base64Util
 * @description	: Base64工具类
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2023/05/02	1.0.0		sheng.zheng		初版
 *
 */
public class Base64Util {
	private static final String charset = "utf-8";
	
	//========================= base64编解码 ======================================
	
	/**
	 * 
	 * @methodName		: base64Decode
	 * @description	: base64解码
	 * @param data		: 待解码的字符串
	 * @return			: 解码后的字符串
	 * @throws Exception : 解码失败，抛出异常
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/01/01	1.0.0		sheng.zheng		初版
	 *
	 */
    public static String base64Decode(String data) throws Exception{
		if (null == data || data.equals("")) {
		    return "";
		}
		
		return new String(Base64.decodeBase64(data.getBytes(charset)), charset);
    }
    
    /**
     * 
     * @methodName		: base64Encode
     * @description	: base64编码
     * @param data		: 待编码的字符串
     * @return			: 编码后的字符串
     * @throws Exception : 编码失败，抛出异常
     * @history		:
     * ------------------------------------------------------------------------------
     * date			version		modifier		remarks                   
     * ------------------------------------------------------------------------------
     * 2021/01/01	1.0.0		sheng.zheng		初版
     *
     */
    public static String base64Encode(String data) throws Exception{
    	if (null == data || data.equals("")) {
	        return "";
	    }
	    return new String(Base64.encodeBase64(data.getBytes(charset)), charset);
    }
    
    /**
     * 
     * @methodName		: bytes2Base64
     * @description	: byte[]转base64
     * @param b			: 待编码的byte数组
     * @return			: 编码后的字符串
     * @throws Exception : 编码失败，抛出异常
     * @history		:
     * ------------------------------------------------------------------------------
     * date			version		modifier		remarks                   
     * ------------------------------------------------------------------------------
     * 2021/01/01	1.0.0		sheng.zheng		初版
     *
     */
    public static String bytes2Base64(byte[] b) throws Exception{
        return Base64.encodeBase64String(b);
    }
    
    /**
     * 
     * @methodName		: base642Bytes
     * @description	: base64转byte[]
     * @param base64Str: 待解码的字符串
     * @return			: 解码后的byte数组
     * @throws Exception : 解码失败，抛出异常
     * @history		:
     * ------------------------------------------------------------------------------
     * date			version		modifier		remarks                   
     * ------------------------------------------------------------------------------
     * 2021/01/01	1.0.0		sheng.zheng		初版
     *
     */
    public static byte[] base642Bytes(String base64Str) throws Exception{
        return Base64.decodeBase64(base64Str);
    } 
}
